# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao_v2.userLoginLogDao import UserLoginLogDao
import time
from . import app

localTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))


CORS(app, supports_credentials=True)

@app.route('/queryUserLoginLogById', methods=['POST'])
def queryUserLoginLogByIdNoToken():

    if not request.json:
        abort(400)
    id = request.json.get("id")
    try:
        userLoginLogJSON, total_count= UserLoginLogDao().queryUserLoginLogById(id)
        if total_count:
            return jsonify({"message": {"code": 1, "status": "queryUserLoginLog OK"},"info":userLoginLogJSON})
        else:
            return jsonify({"info": [], "message": {"code": 0, "status": "No data"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})

@app.route('/queryUserLoginLogByCondition', methods=['POST'])
def queryUserLoginLogByConditionNoToken():
    if not request.json:
        abort(400)
    try:
        page = request.json['page']
        pageIndex = page["pageIndex"]
        pageSize = page["pageSize"]
    except:
        return jsonify({"message": {"code": 0, "status": "请传入 pageIndex和pageSize"}})
    try:
        conditions = request.json['condition']
    except:
        return jsonify({"message": {"code": 0, "status": "未传入指定condition"}})
    try:
        multiSort = request.json['multiSort']
    except:
        return jsonify({"message": "未传入排序规则multiSort"})
    try:
        userLoginLogJSON,total_count = UserLoginLogDao().queryUserLoginLogByCondition(pageIndex=pageIndex, pageSize=pageSize,conditions=conditions, multiSort=multiSort)
        if total_count:
                return jsonify({"message": {"code": 1, "status": "queryUserLoginLog OK"},"total":total_count,"info":userLoginLogJSON})
        else:
            return jsonify({"info": [], "message": {"code": 0, "status": "No data"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})



@app.route('/insertUserLoginLog', methods=['POST'])
def insertUserLoginLogNoToken():

    if not request.json:
        abort(400)

    Info = {
        'userId': request.json.get('userId'),  # 必填，请传递userd
        'userName': request.json.get('userName'),  #
        # 'loginTime': request.json.get('loginTime'),  #
        'loginIp': request.json.get('loginIp'),  #
        'remark': request.json.get('remark'),#
        'loginType':request.json.get('loginType')
    }
    if not Info:
        return jsonify({"message": {"code": 0, "info": "信息填写未完整"}})
    try:
        rs = UserLoginLogDao().addUserLoginLog(Info)
        if rs:
            return jsonify(
                {"massage": {"code": 1, "info": "addUserOauth OK"}})  # 添加函数调用成功，返回添加的信息（不包括userRecharge的Id）
        else:
            return jsonify({"message": {"code": 0, "info": "addUserOauth Error"}})  # 添加函数调用失败


    except Exception, e:
        return jsonify({"message": {"code": 0, "info": str(e)}})



"""
@app.route('/updateUserLoginLog', methods=['POST'])
def updateUserLoginLog():#无需更新

    # 更新zzh_user_recharge信息
    # :return:

    if not request.json:
        abort(400)

    Info = {
        'id':request.json['id'],
        'userId': request.json.get('userId'),  # 必填，请传递userd
        'userName': request.json.get('userName'),  #
        'oauthName': request.json.get('oauthName'),  #
        'oauthAccessToken': request.json.get('oauthAccessToken'),  #
        'oauthOpenId': request.json.get('oauthOpenId')#
    }
    if not Info:
        return jsonify({"message": {"code": 0, "info": "信息填写未完整"}})
    try:
        rs = UserLoginLogDao().updateUserLoginLog(Info)
    except:
        return jsonify({"message": {"code": 0, "info": "信息填写错误"}})
    if rs:
        return jsonify(
            {"info": Info, "massage": {"code": 1, "info": "updateUserOauth OK"}})  # 添加函数调用成功，返回添加的信息
    else:
        return jsonify({"message": {"code": 0, "info": "updateUserOauth Error"}})  # 添加函数调用失败

"""

@app.route('/deleteUserLoginLog', methods=['POST'])  # 根据id数组删除zzh_user_login_log表指定记录,传入格式为 "ids":[1,2,3,4]
def deleteUserLoginLogNoToken():
    ids = request.json.get("ids")
    if not ids:
        return jsonify({"message":"请输入待删除的  id数组！"})
    try:
        rs = UserLoginLogDao().deleteUserLoginLog(ids)
        if rs:
            return jsonify({"message": {"code": 1, "info": "DeleteUserLoginLog OK"}})
        else:
            return jsonify({"message": {"code": 0, "info": "DeleteUserLoginLog Error"}})

    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})

